package de.tmahr.android;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import de.tmahr.android.animation.Diagramm;
import de.tmahr.android.animation.RenderView;
import de.tmahr.android.animation.Diagramm.Abtastung;
import de.tmahr.android.logger.BufferedTextViewLogger;
import de.tmahr.android.logger.Logger;

/**
 * Dieser Test simuliert einen Sensor durch Berührungsereignisse. Die x-Position
 * der Berührung wird als Abtastwert interprertiert und in einem
 * Zeitverlaufsdiagramm @see de.tmahr.android.animation.Diagramm dargestellt.
 */
public class SensorTest2 extends Activity implements SensorEventListener
{
	private static Logger logger;
	private TextView logView;
	private RenderView renderViewX;
	private RenderView renderViewY;
	private RenderView renderViewZ;
	private Diagramm diagrammX;
	private Diagramm diagrammY;
	private Diagramm diagrammZ;
	private SensorManager sensorManager;
	private int sensorType = Sensor.TYPE_ACCELEROMETER;
	private Sensor sensor;

	@Override
	public void onCreate(Bundle b)
	{
		super.onCreate(b);
		setTitle(SensorTest2.class.getSimpleName());
		setContentView(R.layout.sensortest2);

		logView = (TextView) findViewById(R.id.logView);
		logView.setText(SensorTest2.class.getSimpleName());

		logger = new BufferedTextViewLogger(SensorTest2.class.getSimpleName(),
				logView);
		logger.log("Klick mich\n");

		long historyLengthMs = 4000;

		renderViewX = (RenderView) findViewById(R.id.renderViewX);
		renderViewX.setLogger(logger);
		diagrammX = new Diagramm(null,historyLengthMs);
		renderViewX.setSimulation(diagrammX);

		renderViewY = (RenderView) findViewById(R.id.renderViewY);
		renderViewY.setLogger(logger);
		diagrammY = new Diagramm(null,historyLengthMs);
		renderViewY.setSimulation(diagrammY);

		renderViewZ = (RenderView) findViewById(R.id.renderViewZ);
		renderViewZ.setLogger(logger);
		diagrammZ = new Diagramm(null,historyLengthMs);
		renderViewZ.setSimulation(diagrammZ);

		sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
		if(sensorManager.getSensorList(sensorType).size()==0)
		{
			logger.log("Es gibt den Sensor nicht.\n");
			sensor = null;
		}
		else
		{
			sensor = sensorManager.getSensorList(sensorType).get(0);
		}		
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		renderViewX.pause();
		renderViewY.pause();
		renderViewZ.pause();
		
		if(sensor != null)
		{
			sensorManager.unregisterListener(this,sensor);
			logger.log("Von Sensor abgemeldet.\n");
		}			
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		renderViewX.resume();
		renderViewY.resume();
		renderViewZ.resume();
		
		if(sensor != null)
		{
			if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME))
			{
				logger.log("Bei Sensor angemeldet.\n");
			}
			else
			{
				logger.log("Kann dem Sensor keinen Beobachter hinzufügen\n");
			}
		}				
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {}

	@Override
	public void onSensorChanged(SensorEvent event)
	{
		diagrammX.fuegeAbtastwertHinzu(new Abtastung(event.timestamp/1000000,event.values[0]));
		diagrammY.fuegeAbtastwertHinzu(new Abtastung(event.timestamp/1000000,event.values[1]));
		diagrammZ.fuegeAbtastwertHinzu(new Abtastung(event.timestamp/1000000,event.values[2]));
	}	
}
